package com.jiangyg.devtools.execute.code;

import com.intellij.openapi.project.Project;
import com.intellij.ui.components.JBScrollPane;
import com.jiangyg.devtools.entity.code.TemplateDefinition;
import com.jiangyg.devtools.toolkit.CacheUtils;
import com.jiangyg.devtools.ui.table.AbstractEditableTableModel;
import com.jiangyg.devtools.ui.table.EditableTable;

import javax.swing.event.TableModelEvent;
import java.util.List;

/**
 * 类描述：代码模板表格
 *
 * @author jiangyg
 * @date 2023-08-23
 */
public class CodeTemplateTablePanel extends JBScrollPane {

    private final MyTableModel tableModel = new MyTableModel();

    public CodeTemplateTablePanel(Project project) {
        super();
        this.setViewportView(new EditableTable("code-template", tableModel, project));
    }

    /**
     * 功能描述：加载表格数据
     */
    public void loadTableData(List<TemplateDefinition> templates) {
        tableModel.emptyAllRows();
        for (TemplateDefinition template : templates) {
            tableModel.addRow(template);
        }
        tableModel.fireTableDataChanged();
    }

    private static class MyTableModel extends AbstractEditableTableModel<TemplateDefinition> {

        @Override
        public void fireTableChanged(TableModelEvent e) {
            super.fireTableChanged(e);
            CacheUtils.setCodeTemplate(this.getDataList());
        }

    }

}
